// pages/reback/reback.js
const app = getApp();
import { makeReback, reback } from '../../config/https.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    counts: 0, // 字数
    upImgs: [], // 凭证列表
    reason: '', // 原因
    show: false, // 是否展示弹窗
    showYun: true, // 是否展示运费
    goodsType: 1, // 单商品 2多商品
    order_id: '', // 订单id
    infos: '', // 数据信息
    rebackMoney: '', // 退款金额
    content: '', // 说明
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      order_id: options.orderId
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

  // 获取数据
  getData(){
    app.loading();
    let that = this;
    reback({
      token: app.globalData.userId,
      order_id: this.data.order_id
    }).then(res =>{
      wx.hideLoading();
      if(res.code == 1){
        if (res.data.goods.length > 1) {
          res.data.goods.forEach((item) => {
            item.check = true
          })
        }

        that.setData({
          infos: res.data,
          goodsType: res.data.goods.length > 1 ? 2 : 1,
          rebackMoney: res.data.pay_price
        })

        
      }else{
        app.$tip(res.msg);
      }
    })
  },

  // 说明
  initText(e){
    this.setData({
      content: e.detail.value,
      counts: e.detail.value.length
    })
  },



  // 选择商品
  change(e){
    let index = e.currentTarget.dataset.index,
      old = this.data.infos;
    old.goods[index].check = !old.goods[index].check;
    this.setData({
      infos: old
    })

    let price = 0,len = 0;
    this.data.infos.goods.forEach((item) =>{
      if(item.check){
        len = Number(len) + 1;
        price = Number(price) + Number(item.refund_money)
      }
    })
    if (len == this.data.infos.goods.length){
      this.setData({
        showYun: true
      })
      price = Number(this.data.infos.pay_price)
    }else{
      this.setData({
        showYun: false
      })
    }
    this.setData({
      rebackMoney: price.toFixed(2)
    })
  },

  // 上传图片
  addimg(){
    let that = this,old = this.data.upImgs;
    app.loading();
    wx.chooseImage({
      count: 5,
      success: function(res) {
        let tempFilePaths = res.tempFilePaths;
        // 多图
        if (res.tempFilePaths.length > 1) {
          let len = tempFilePaths.length;
          tempFilePaths.forEach((item, index) => {
            wx.uploadFile({
              url: app.baseURL + 'Upload/upload',
              filePath: tempFilePaths[index],
              name: 'file',
              formData: {
                file: tempFilePaths[index],
              },
              success(data) {
                let infos = JSON.parse(data.data);
                wx.hideLoading();
                if(infos.code == 1){
                  old.push(infos.data)
                  that.setData({
                    upImgs: old
                  })
                } else {
                  app.$tip(infos.msg);
                }

              }
            })
          })
        } else {
          // 单图
          wx.uploadFile({
            url: app.baseURL + 'Upload/upload',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              file: tempFilePaths[0],
            },
            success(data) {
              let infos = JSON.parse(data.data);
              wx.hideLoading();
              if (infos.code == 1) {
                old.push(infos.data)
                that.setData({
                  upImgs: old
                })
              } else {
                app.$tip(infos.msg);
              }

            }
          })
        }
      },
    })
  },

  // 删除图片
  delThis(e){
    let index = e.currentTarget.dataset.index,old = this.data.upImgs;
    old.splice(index, 1);
    this.setData({
      upImgs: old
    })
  },

  // 展示弹窗
  choose(){
    this.setData({
      show: true
    })
  },

  // 选择原因
  chooseThis(e){
    this.setData({
      reason: e.currentTarget.dataset.name
    })
  },

  // 取消
  canceled(){
    this.setData({
      show: false
    })
  },

  // 确定
  sured(){
    if(!this.data.reason){
      app.$tip('请选择原因!');
      return
    }
    this.setData({
      show: false
    })
  },

  // 提交
  finished(){
    let that = this,len = 0, goodsId = [];
    // 多商品判断是否已选择要退款的商品
    if(this.data.goodsType == 2){
      this.data.infos.goods.forEach((item) =>{
        if(item.check){
          len++;
          goodsId.push(item.id)
        }
      })
      if(len < 1){
        app.$tip('请选择要退款的商品!');
        return
      }
    }

    if(!this.data.reason){
      app.$tip('请选择退款原因!');
      return
    }

    app.loading();
    makeReback({
      token: app.globalData.userId,
      order_id: that.data.order_id,
      order_goods_id: that.data.goodsType == 2 ? goodsId.toString() : that.data.infos.goods[0].id,
      remark: that.data.reason,
      description: that.data.content,
      img: that.data.upImgs.toString()
    }).then(res =>{
      wx.hideLoading();
      if(res.code == 1){
        app.$tip(res.msg);
        setTimeout(() =>{
          wx.redirectTo({
            url: '/pages/rebackOrder/rebackOrder',
          })
        },1200)
      }else{
        app.$tip(res.msg);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})